<!--财务设置->会计科目-->
<template>
    <div class="content-wrapper a-base-container">
        <div class="business-big-box" style="margin:2px 8px;">
            <div class="btn-wrapper clearfix">
                <a-row class="text-center clearfix">
                    <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                        <a-col :span="2">
                            <div class="btns" @click="add" v-if="permissions.includes('accounting:add')">
                                <a-icon class="btns-icon" type="file-add" />
                                <div class="btns-text">新增</div>
                            </div>
                        </a-col>
                        <a-col :span="2" v-if="permissions.includes('accounting:edit')">
                            <div class="btns" @click="edit">
                                <a-icon class="btns-icon" type="edit" />
                                <div class="btns-text">修改</div>
                            </div>
                        </a-col>
                        <a-col :span="2" v-if="permissions.includes('accounting:delete')">
                            <div class="btns" @click="remove">
                                <a-icon class="btns-icon" type="delete" />
                                <div class="btns-text">删除</div>
                            </div>
                        </a-col>
                        <!-- <a-col :span="2">
                            <div class="btns" @click="()=>{this.$message.warning('暂未开发')}">
                                <a-icon class="btns-icon" type="import" />
                                <div class="btns-text">导入</div>
                            </div>
                        </a-col>
                        <a-col :span="2">
                            <div class="btns" @click="()=>{this.$message.warning('暂未开发')}">
                                <a-icon class="btns-icon" type="setting" />
                                <div class="btns-text">字段设置</div>
                            </div>
                        </a-col> -->
                    </a-col>
                </a-row>
            </div>
            <div class="course-table-container">
                <section>
                    <a-tabs default-active-key="1" @change="onSelectTree">
                        <a-tab-pane v-for="item in treeData[0].children" :key="item.id" :tab="item.title">

                        </a-tab-pane>
                    </a-tabs>
                    <div style="height: calc(100vh - 200px);overflow-y: auto;">
                        <a-table id="table_course" :columns="columns" :data-source="data" bordered
                            :row-selection="{ selectedRowKeys: selectedKeys, onChange: selection, columnWidth: 35 }"
                            :pagination="false" :scroll="{ x: 1000, y: 0 }">
                            <div slot="filterDropdownName"
                                slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
                                style="padding: 8px">
                                <a-input :placeholder="`搜索${column.title}`" v-model="searchName"
                                    style="width: 188px; margin-bottom: 8px; display: block;"
                                    @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
                                    @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)" />
                                <a-button type="primary" icon="search" size="small"
                                    style="width: 90px; margin-right: 8px"
                                    @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)">
                                    搜索
                                </a-button>
                                <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
                                    重置
                                </a-button>
                            </div>
                            <div slot="filterDropdownCode"
                                slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
                                style="padding: 8px">
                                <a-input :placeholder="`搜索${column.title}`" v-model="searchCode"
                                    style="width: 188px; margin-bottom: 8px; display: block;"
                                    @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
                                    @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)" />
                                <a-button type="primary" icon="search" size="small"
                                    style="width: 90px; margin-right: 8px"
                                    @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)">
                                    搜索
                                </a-button>
                                <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
                                    重置
                                </a-button>
                            </div>
                            <a-icon slot="filterIcon" type="filter" theme="filled"
                                :style="{ color: '#444', marginRight: '4px' }" />
                        </a-table>
                    </div>

                </section>
                <!-- <section class="course-tree-box">
                    <a-tree :tree-data="treeData" default-expand-all @select="onSelectTree" show-line>
                        <a-icon slot="switcherIcon" type="down" />
                    </a-tree>
                </section>
                <section class="course-table-box">
                    <a-table id="table_course" :columns="columns" :data-source="data" bordered :row-selection="{selectedRowKeys: selectedKeys, onChange: selection,columnWidth:35}" :pagination="false" :scroll="{ x: 1000, y: 0}">
                        <div slot="filterDropdownName" slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }" style="padding: 8px">
                            <a-input :placeholder="`搜索${column.title}`" v-model="searchName" style="width: 188px; margin-bottom: 8px; display: block;" @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])" @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)" />
                            <a-button type="primary" icon="search" size="small" style="width: 90px; margin-right: 8px" @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)">
                                搜索
                            </a-button>
                            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
                                重置
                            </a-button>
                        </div>
                        <div slot="filterDropdownCode" slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }" style="padding: 8px">
                            <a-input :placeholder="`搜索${column.title}`" v-model="searchCode" style="width: 188px; margin-bottom: 8px; display: block;" @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])" @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)" />
                            <a-button type="primary" icon="search" size="small" style="width: 90px; margin-right: 8px" @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)">
                                搜索
                            </a-button>
                            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
                                重置
                            </a-button>
                        </div>
                        <a-icon slot="filterIcon" type="filter" theme="filled" :style="{ color: '#444',marginRight:'4px' }" />
                    </a-table>
                </section> -->
            </div>

            <a-modal v-model="parentModal" title="上级会计科目" centered width="650px">
                <div style="height: 400px;overflow-y:scroll;">
                    <a-table :columns="parentColumns" :data-source="data" bordered
                        :row-selection="{ selectedRowKeys: parentSelectedKeys, onChange: parentSelection, type: 'radio' }"
                        :pagination="false">
                    </a-table>
                </div>
                <template slot="footer">
                    <a-button type="primary" html-type="submit" @click="selectParentData">确定</a-button>
                    <a-button :style="{ marginLeft: '8px' }" @click="() => { parentModal = false }">关闭</a-button>
                </template>
            </a-modal>

            <drag-modal class="course-ant-modal-body" :footer="false"
                @cancel="() => { editModal = false; this.selectedKeys = []; this.selectedData = []; }" v-model="editModal"
                :title="editItem.id ? '编辑会计科目' : '新增会计科目'" centered width="800px">
                <div id="components-form-demo-advanced-search">
                    <div class="business-big-box" style="border:0px;">
                        <div class="btn-wrapper clearfix" style="border-bottom:1px solid #ccc;">
                            <a-row class="text-center clearfix">
                                <a-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                                    <a-col :span="3">
                                        <div class="btns" @click="handleSubmit">
                                            <a-icon class="btns-icon" type="save" />
                                            <div class="btns-text">保存</div>
                                        </div>
                                    </a-col>
                                    <a-col :span="3">
                                        <div class="btns" @click="handleSubmit(1)">
                                            <a-icon class="btns-icon" type="file-add" />
                                            <div class="btns-text">保存并新增</div>
                                        </div>
                                    </a-col>
                                    <!-- <a-col :span="3" v-if="copyBtn">
                                        <div class="btns" @click="deleteId">
                                            <a-icon class="btns-icon" type="copy" />
                                            <div class="btns-text">复制</div>
                                        </div>
                                    </a-col> -->
                                    <a-col :span="3">
                                        <div class="btns" @click="page(3)">
                                            <a-icon class="btns-icon" type="vertical-right" />
                                            <div class="btns-text">首张</div>
                                        </div>
                                    </a-col>

                                    <a-col :span="3">
                                        <div class="btns" @click="page(1)">
                                            <a-icon class="btns-icon" type="left" />
                                            <div class="btns-text">上张</div>
                                        </div>
                                    </a-col>

                                    <a-col :span="3">
                                        <div class="btns" @click="page(2)">
                                            <a-icon class="btns-icon" type="right" />
                                            <div class="btns-text">下张</div>
                                        </div>
                                    </a-col>

                                    <a-col :span="3">
                                        <div class="btns" @click="page(4)">
                                            <a-icon class="btns-icon" type="vertical-left" />
                                            <div class="btns-text">末张</div>
                                        </div>
                                    </a-col>
                                </a-col>
                            </a-row>
                        </div>
                    </div>
                    <a-form class="ant-advanced-search-form" :form="form" :label-col="{ span: 8 }"
                        :wrapper-col="{ span: 16 }" @submit="handleSubmit">
                        <a-row :gutter="[16, 0]">
                            <a-col :span="12">
                                <a-form-item label="上级科目">
                                    <a-input-search :disabled="editItem.id" v-model="editItem.parentName"
                                        placeholder="请选择上级科目" read-only @click="() => { this.parentModal = true }"
                                        @search="() => { this.parentModal = true }" />
                                </a-form-item>
                            </a-col>
                            <a-col :span="12">
                                <a-form-item label="编码规则">
                                    {{ ruleCode }}
                                </a-form-item>
                            </a-col>
                            <a-col :span="12">
                                <a-form-item label="科目编码">
                                    <a-input :disabled="editItem.id" autocomplete="off"
                                        v-decorator="['code', { rules: [{ required: true, message: '科目编码不能为空' }] }]"
                                        placeholder="请输入科目编码" />
                                </a-form-item>
                            </a-col>

                            <a-col :span="12">
                                <a-form-item label="科目名称">
                                    <a-input autocomplete="off"
                                        v-decorator="['name', { rules: [{ required: true, message: '科目名称不能为空' }] }]"
                                        placeholder="请输入科目名称" />
                                </a-form-item>
                            </a-col>
                            <a-col :span="12">
                                <a-form-item label="科目类型">
                                    <!-- v-model="editItem.accountingTypeId" -->
                                    <a-select @change="handleChange"
                                        v-decorator="['accountingTypeId', { rules: [{ required: true, message: '科目类型不能为空' }] }]"
                                        placeholder="请选择科目类型" :disabled="editItem.parentId || editItem.id">
                                        <a-select-option v-for="item in treeData[0].children" :key="Number(item.id)"
                                            :value="Number(item.id)">
                                            {{ item.title }}
                                        </a-select-option>
                                    </a-select>
                                </a-form-item>
                            </a-col>
                            <a-col :span="12">
                                <a-form-item label="余额方向">
                                    <a-select v-model="editItem.dcDirection" placeholder="请选择余额方向">
                                        <a-select-option :value="1">借方</a-select-option>
                                        <a-select-option :value="2">贷方</a-select-option>
                                    </a-select>
                                </a-form-item>
                            </a-col>
                            <a-col :span="12">
                                <a-form-item label="账页格式">
                                    <a-select v-model="editItem.bookFormat">
                                        <a-select-option :value="1">金额式</a-select-option>
                                        <a-select-option :value="2">外币金额式</a-select-option>
                                        <a-select-option :value="3">数量金额式</a-select-option>
                                        <a-select-option :value="4">外币数量式</a-select-option>
                                    </a-select>
                                </a-form-item>
                            </a-col>
                        </a-row>
                        <a-row>
                            <a-col :span="12">
                                <card-wrapper :title="'现金银行'" style="padding-right:0">
                                    <template v-slot:slot-1>
                                        <a-checkbox-group v-model="cashAccountCheck" @change="cashAccount">
                                            <a-checkbox value="a" :disabled="a">现金科目</a-checkbox>
                                            <a-checkbox value="b" :disabled="b">银行科目</a-checkbox>
                                            <a-checkbox value="c" :disabled="c">现金等价物科目</a-checkbox>
                                            <a-checkbox value="d" :disabled="d">日记账</a-checkbox>
                                            <a-checkbox value="e" :disabled="e">填制凭证时录入结算信息</a-checkbox>
                                        </a-checkbox-group>
                                    </template>
                                </card-wrapper>
                            </a-col>
                            <a-col :span="12">
                                <card-wrapper :title="'辅助核算'" style="padding-right:0;">
                                    <template v-slot:slot-1>
                                        <div style="height:92px;padding-top:15px;">
                                            <a-checkbox v-model="editItem.isAuxaccDepartment" :disabled="auxiliaryA"
                                                @change="auxiliaryCheck('a')">部门核算</a-checkbox>
                                            <a-checkbox v-model="editItem.isAuxaccPerson" :disabled="auxiliaryB"
                                                @change="auxiliaryCheck('b')">个人往来</a-checkbox>
                                            <a-checkbox v-model="editItem.isAuxaccCustomer" :disabled="auxiliaryC"
                                                @change="auxiliaryCheck('c')">客户往来</a-checkbox>
                                            <a-checkbox v-model="editItem.isAuxaccSupplier" :disabled="auxiliaryD"
                                                @change="auxiliaryCheck('d')">供应商往来</a-checkbox>
                                            <a-checkbox v-model="editItem.isAuxaccProject" @change="auxiliaryCheck()">
                                                项目核算</a-checkbox>
                                            <!-- <a-checkbox value="1" v-model="editItem.isAuxaccInventory">物料</a-checkbox> -->
                                        </div>
                                    </template>
                                </card-wrapper>
                            </a-col>
                            <a-col :span="12">
                                <card-wrapper :title="'外币核算'" style="padding-right:0">
                                    <template v-slot:slot-1>
                                        <a-checkbox value="1" v-model="editItem.isExchange">启用核算</a-checkbox>
                                        <a-checkbox value="1" :disabled="!editItem.isExchange"
                                            v-model="editItem.isFinalTransferExchange">期末调汇</a-checkbox>
                                        <a-form-item label="默认币种">
                                            <a-select v-model="editItem.defaultCurrencyId"
                                                :disabled="!editItem.isExchange" @change="handlerChangeCurrency"
                                                placeholder="请选择币种">
                                                <a-select-option :value="item.id" v-for="(item, index) in currencyData"
                                                    :key="index">{{ item.name }}</a-select-option>
                                            </a-select>
                                        </a-form-item>
                                    </template>
                                </card-wrapper>
                            </a-col>

                            <a-col :span="12">
                                <card-wrapper :title="'数量核算'" style="padding-right:0">
                                    <template v-slot:slot-1>
                                        <a-checkbox value="1" v-model="editItem.isQuantity">启用核算</a-checkbox>
                                        <a-form-item label="计量单位">
                                            <a-select v-model="editItem.unitId" :disabled="!editItem.isQuantity"
                                                @change="handlerChangeUnit" placeholder="请选择计量单位">
                                                <a-select-option :value="item.id" v-for="(item, index) in unitData"
                                                    :key="index">{{ item.name }}</a-select-option>
                                            </a-select>
                                        </a-form-item>
                                    </template>
                                </card-wrapper>
                            </a-col>

                        </a-row>
                    </a-form>
                </div>
                <!-- <template slot="footer">
                    <a-button type="primary" html-type="submit" @click="handleSubmit">保存</a-button>
                    <a-button :style="{ marginLeft: '8px' }" @click="() => {editModal = false}">关闭</a-button>
                </template> -->
            </drag-modal>
        </div>
    </div>
</template>
<script>

import { mapState } from "vuex";
import table from "@/mixins/table";
import { pageList, save, deleteAccount, getAccount, pageRun } from "@/api/erp/accounting";
import { list } from "@/api/erp/currency";
import { unitList } from "@/api/inventory/unit";
import { getDetail } from "@/api/erp/erpGradeRule";
import { cardWrapper } from "@/components/CardWrapper";
import { Controlwidth } from "@/utils/antTableComponents";
let keyMap = new Map();

export default {
    mixins: [table],
    components: {
        cardWrapper,
    },
    updated () {
        Controlwidth("table_course", 50);
    },
    data () {
        const parentColumns = [
            {
                title: "级次",
                dataIndex: "depth",
                key: "officeName",
                align: "left",
                width: 50
            },
            {
                title: "科目名称",
                dataIndex: "name",
                key: "officeName",
                align: "left",
                width: 100,
                customRender: (text, row, index) => {
                    if (row.depth != 1) {
                        let lelft = 8 * row.depth + "px";
                        let style = { marginLeft: lelft };
                        return <span style={style} >{text}</span>;
                    } else {
                        return <span>{text}</span>;
                    }
                }
            },
            {
                title: "科目编码",
                dataIndex: "code",
                key: "officeName",
                align: "left",
                width: 100,
            },
        ];

        const columns = [
            {
                title: "级次",
                dataIndex: "depth",
                key: "depth",
                align: "center",
                width: 40
            },
            {
                title: "科目编码",
                dataIndex: "code",
                key: "code",
                align: "left",
                width: 100,
                scopedSlots: {
                    filterDropdown: 'filterDropdownCode',
                    filterIcon: 'filterIcon',
                },
            },
            {
                title: "科目名称",
                dataIndex: "name",
                key: "name",
                align: "left",
                width: 100,
                scopedSlots: {
                    filterDropdown: 'filterDropdownName',
                    filterIcon: 'filterIcon',
                },
                customRender: (text, row, index) => {
                    if (row.depth != 1) {
                        let lelft = 8 * row.depth + "px";
                        let style = { marginLeft: lelft };
                        return <span style={style} >{text}</span>;
                    } else {
                        return <span>{text}</span>;
                    }
                }
            },
            {
                title: "科目类型",
                width: 60,
                dataIndex: "accountingTypeName",
                key: "accountingTypeName",
                align: "center",
            },
            {
                title: "余额方向",
                width: 60,
                dataIndex: "dcDirection",
                key: "dcDirection",
                align: "center",
                customRender: (text, row, index) => {
                    if (text) {
                        return text == 1 ? "借方" : text == 2 ? "贷方" : "";
                    }
                }
            },
            {
                title: "现金银行",
                dataIndex: "bank",
                key: "bank",
                align: "left",
                width: 120,
                ellipsis: true,
                customRender: (text, row, index) => {
                    let content = [];
                    if (row.isCash) {
                        content.push("现金科目");
                    }
                    if (row.isBank) {
                        content.push("银行科目");
                    }
                    if (row.isCashEquivalents) {
                        content.push("现金等价物科目");
                    }
                    if (row.isDayBook) {
                        content.push("日记账");
                    }
                    if (row.isBillingInfo) {
                        content.push("填制凭证时录入结算信息");
                    }
                    return content.join('，');
                }
            },
            {
                title: "外币核算",
                width: 60,
                dataIndex: "defaultCurrencyName",
                key: "defaultCurrencyName",
                align: "center",
            },
            {
                title: "数量核算",
                dataIndex: "unitName",
                key: "unitName",
                align: "center",
                width: 80
            },
            {
                title: "辅助核算项",
                dataIndex: "officeName",
                key: "officeName",
                align: "left",
                width: 100,
                ellipsis: true,
                customRender: (text, row, index) => {
                    if (row.isEndNode) {
                        let content = [];
                        if (row.isAuxaccDepartment) {
                            content.push("部门核算");
                        }
                        if (row.isAuxaccPerson) {
                            content.push("人个往来");
                        }
                        if (row.isAuxaccCustomer) {
                            content.push("客户往来");
                        }
                        if (row.isAuxaccSupplier) {
                            content.push("供应商往来");
                        }
                        if (row.isAuxaccProject) {
                            content.push("项目核算");
                        }
                        if (row.isAuxaccInventory) {
                            content.push("物料");
                        }
                        return content.join('，');
                    } else {
                        return '';
                    }
                }
            },
            {
                title: "账页格式",
                dataIndex: "bookFormat",
                key: "bookFormat",
                align: "left",
                width: 80,
                customRender: (text, row, index) => {
                    if (text) {
                        const book = ["金额式", "外币金额式", "数量金额式", "外币数量式"]
                        return book[text - 1];
                    }
                }
            },
        ];

        return {
            a: false, b: false, c: false, d: false, e: false,
            auxiliaryA: false,
            auxiliaryB: false,
            auxiliaryC: false,
            auxiliaryD: false,
            auxiliaryE: false,

            form: this.$form.createForm(this, { name: "advanced_search" }),
            selectedKeys: [],
            parentSelectedKeys: [],
            selectedData: {},
            parentSelectedData: {},
            selectedIds: [],
            columns,
            parentColumns,
            data: [],
            editModal: false,
            parentModal: false,
            cashAccountCheck: [],
            accountingTypeName: '',//科目类型名称
            type: 1,
            currentId: null,//当前条id
            pagination: {
                size: "small",
                defaultPageSize: -1,
                current: 1,
                pageSize: -1,
                total: 0,
                showTotal: total => {
                    return (
                        <div>
                            共 {total} 条数据，每页
                            <a-input style="height:22px !important;width:3.2rem;"
                                value={this.pagination.pageSize}
                                min={1}
                                max={10}
                                size="small"
                                step={1}
                                onPressEnter={e => this.changePageSize(e)}
                            />
                            条
                        </div>
                    );
                },
                onChange: (page, pageSize) => {
                    this.pagination.current = page;
                    this.PageList();
                },
            },
            editItem: {
                accountingTypeId: "", //科目类型
                bookFormat: 1, //账页格式
                dcDirection: 1, //余额方向
                type: "",
                parentId: null, //上级id
                parentName: "", //上级名称
                isBillingInfo: null,
                isBank: null,//银行科目
                isCash: null,//现金科目
                isCashEquivalents: null,//现金等价物科目
                isDayBook: null,//日记账
                defaultCurrencyId: null,
                defaultCurrencyName: null,
                unitId: null,
                unitName: null,
            },
            currencyData: [],
            ruleCode: "",
            postData: { accountingTypeId: "" },
            searchName: null,
            searchCode: null,
            treeData: [{
                title: '科目分类',
                key: '',
                children: [],
            }]
        }
    },
    activated () {
        this.getCurrencyList();
        this.getUnitList();
    },
    created () {
        this.PageList();
        this.getCurrencyList();
        this.getUnitList();
        this.getCodeDetail();

        this.getAccount();//获取科目分类
    },
    computed: {
        ...mapState({
            userInfo: state => state.user.info,
            permissions: state => state.user.permissions,
        }),

    },
    methods: {
        //获取科目分类
        getAccount () {
            getAccount().then(res => {
                if (res.code === 200) {

                    let data = res.data.map(v => {
                        return { ...v, isLeaf: true, title: v.name, key: v.id }
                    })
                    this.treeData[0].children = data;

                    this.treeData[0].children.unshift({ id: null, title: '全部' })
                    console.log(data)
                }
            })
        },
        page (p) {
            //翻页
            pageRun({ actionType: p, currentId: this.currentId }).then(response => {
                if (response.code === 200) {
                    if ((p == 1 || p == 3) && response.data == null) {
                        this.$message.warning('已经是首页了');
                        return
                    }

                    if ((p == 2 || p == 4) && response.data == null) {
                        this.$message.warning('已经是末页了');
                        return
                    }

                    this.currentId = response.data.id;
                    this.editItem = response.data;
                    this.accountingTypeName = response.data.accountingTypeName;
                    this.editItem.parentName = keyMap.get(this.editItem.parentId);
                    this.cashAccountCheck = [];
                    //现金银行
                    if (this.editItem.isCash) {
                        this.cashAccountCheck.push('a');
                    }
                    if (this.editItem.isBank) {
                        this.cashAccountCheck.push('b');
                    }
                    if (this.editItem.isCashEquivalents) {
                        this.cashAccountCheck.push('c');
                    }
                    if (this.editItem.isDayBook) {
                        this.cashAccountCheck.push('d');
                    }
                    if (this.editItem.isBillingInfo) {
                        this.cashAccountCheck.push('e');
                    }
                    console.log(this.cashAccountCheck);
                    this.cashAccount(this.cashAccountCheck);
                    this.form = this.$form.createForm(this, {
                        mapPropsToFields: () => {
                            return {
                                name: this.$form.createFormField({
                                    value: this.editItem.name
                                }),
                                code: this.$form.createFormField({
                                    value: this.editItem.code
                                }),
                                accountingTypeId: this.$form.createFormField({
                                    value: this.editItem.accountingTypeId
                                }),

                            };
                        }
                    });

                }
            }).catch(error => { });
        },
        // 获取列表
        PageList () {
            const { postData } = this;
            postData.current = this.pagination.current;
            postData.size = this.pagination.pageSize;
            if (this.searchCode) {
                postData.code = this.searchCode;
                postData.name = null;
            } else if (this.searchName) {
                postData.name = this.searchName;
                postData.code = null;
            } else {
                postData.name = null;
                postData.code = null;
            }
            pageList(postData).then(response => {
                if (response.code === 200) {
                    const { data } = response;
                    const { records, total, current, size } = data;

                    this.data = records.map(v => {
                        return { ...v, key: v.id }
                    });
                    this.pagination.total = total;

                    for (let i = 0; i < this.data.length; i++) {
                        const node = this.data[i];
                        keyMap.set(node.id, node.name);
                    }
                }
            }).catch(error => { });
        },
        handlerChangeCurrency (key, val) {
            console.log(key, val)
            this.editItem.defaultCurrencyName = val.componentOptions.children[0].text;
        },
        handlerChangeUnit (key, val) {
            console.log(key, val);
            this.editItem.unitName = val.componentOptions.children[0].text;
        },
        //树形菜单
        onSelectTree (keys, event) {
            console.log(keys, event)
            this.postData.accountingTypeId = keys;
            this.PageList();
        },
        //获取外币
        getCurrencyList () {
            list({ isBaseCurrency: false }).then(res => {
                this.currencyData = res.data;
            });
        },
        //获取计量单位
        getUnitList () {
            unitList().then(res => {
                this.unitData = res.data;
            });
        },
        handleChange (key, value) {
            console.log(key, value);
            this.accountingTypeName = value.componentOptions.children[0].text;
        },
        handleSubmit (type) {
            this.form.validateFields((err, values) => {
                this.editItem.name = values.name;
                this.editItem.code = values.code;
                this.editItem.accountingTypeId = values.accountingTypeId;
                this.editItem.accountingTypeName = this.accountingTypeName;
                this.editItem.createdTime = null;
                //现金银行
                console.log(this.cashAccountCheck);

                this.cashAccountCheck.some(e => e == 'a') ? this.editItem.isCash = true : this.editItem.isCash = false;
                this.cashAccountCheck.some(e => e == 'b') ? this.editItem.isBank = true : this.editItem.isBank = false;
                this.cashAccountCheck.some(e => e == 'c') ? this.editItem.isCashEquivalents = true : this.editItem.isCashEquivalents = false;
                this.cashAccountCheck.some(e => e == 'd') ? this.editItem.isDayBook = true : this.editItem.isDayBook = false;
                this.cashAccountCheck.some(e => e == 'e') ? this.editItem.isBillingInfo = true : this.editItem.isBillingInfo = false;

                //如果启用外币（外币必须选择）
                if (this.editItem.isExchange) {
                    if (this.editItem.defaultCurrencyId == null || this.editItem.defaultCurrencyId == '') {
                        this.$message.warning('启用外币核算时，必须选择币种');
                        return;
                    }
                }
                //如果数量核算（计量单位必须选择）
                if (this.editItem.isQuantity) {
                    if (this.editItem.unitId == null || this.editItem.unitId == '') {
                        this.$message.warning('启用数量核算时，必须选择计量单位');
                        return;
                    }
                }

                if (!err) {
                    save(this.editItem).then(response => {
                        if (response.code === 200) {

                            this.$message.success('保存成功');
                            //刷新 列表

                            this.PageList();

                            if (type == 1) {
                                this.add();
                                this.selectedKeys = [];
                                this.selectedData = [];
                            }

                        }
                    })
                }
            });
        },
        add () {
            this.editModal = true;
            this.parentSelectedKeys = [];
            this.parentSelectedData = {};
            this.auxiliaryA = false;
            this.auxiliaryB = false;
            this.auxiliaryC = false;
            this.auxiliaryD = false;
            this.auxiliaryE = false;
            this.cashAccountCheck = [];
            this.a = false; this.b = false; this.c = false; this.d = true; this.e = true;
            this.editItem = {
                bookFormat: 1, //账页格式
                dcDirection: 1, //余额方向
                type: "",
                parentId: null, //上级id
                parentName: null, //上级名称
            };
            this.form = this.$form.createForm(this, {
                mapPropsToFields: () => {
                    return {
                        name: this.$form.createFormField({
                            value: ""
                        }),
                        accountingTypeId: this.$form.createFormField({
                            value: ""
                        }),
                        code: this.$form.createFormField({
                            value: ""
                        })
                    };
                }
            });
        },
        //编辑
        edit () {
            if (this.selectedKeys.length != 1) {
                this.$message.warning("请选择一条数据");
                return;
            }
            this.currentId = this.selectedKeys[0];

            console.log(keyMap);
            this.editModal = true
            this.editItem = { ...this.selectedData };
            this.accountingTypeName = this.editItem.accountingTypeName;
            this.editItem.type = '1';
            this.editItem.parentName = keyMap.get(this.editItem.parentId);
            this.cashAccountCheck = [];
            //现金银行
            if (this.editItem.isCash) {
                this.cashAccountCheck.push('a');
            }
            if (this.editItem.isBank) {
                this.cashAccountCheck.push('b');
            }
            if (this.editItem.isCashEquivalents) {
                this.cashAccountCheck.push('c');
            }
            if (this.editItem.isDayBook) {
                this.cashAccountCheck.push('d');
            }
            if (this.editItem.isBillingInfo) {
                this.cashAccountCheck.push('e');
            }
            this.cashAccount(this.cashAccountCheck);

            //辅助核算
            this.auxiliaryA = false;
            this.auxiliaryB = false;
            this.auxiliaryC = false;
            this.auxiliaryD = false;
            this.auxiliaryE = false;
            this.auxiliaryCheck();


            this.form = this.$form.createForm(this, {
                mapPropsToFields: () => {
                    return {
                        name: this.$form.createFormField({
                            value: this.editItem.name
                        }),
                        code: this.$form.createFormField({
                            value: this.editItem.code
                        }),
                        accountingTypeId: this.$form.createFormField({
                            value: this.editItem.accountingTypeId
                        }),

                    };
                }
            });
        },
        //删除
        remove () {
            if (this.selectedKeys.length != 1) {
                this.$message.warning("请选择一条数据");
                return;
            }

            let _this = this;
            let id = this.selectedKeys[0];
            _this.$confirm({
                title: "确认",
                content: (
                    <span>
                        您确定要{<span style="color:red;">删除</span>}该条单据吗？
                    </span>
                ),
                onOk () {
                    deleteAccount({ "id": id }).then(res => {
                        if (res.code == 200) {
                            _this.PageList();
                            _this.selectedKeys = [];
                            _this.selectedData = [];
                            _this.$message.success("删除成功");
                        } else {
                            _this.$message.error(res.message);
                        }
                    });
                },
                onCancel () { },
            });

            // this.$nlyaModal.msgBoxConfirm("您确定要删除该会计科目吗？", {
            //     title: "请确认是否需要马上删除",
            //     size: "sm",
            //     buttonSize: "md",
            //     okTitle: "是",
            //     cancelTitle: "否",
            //     titleTag: "h6",
            //     headerClass: "p-2",
            //     footerClass: "p-2",
            //     hideHeaderClose: false,
            //     centered: true
            // }).then(value => {
            //     if (value) {
            //         deleteAccount(this.selectedIds).then(res => {
            //             if (res.code == 200) {
            //                 this.PageList();
            //                 this.selectedKeys = [];
            //                 this.selectedData = [];
            //                 this.$message.success("删除成功");
            //             } else {
            //                 this.$message.error(res.message);
            //             }
            //         });
            //     }
            // });
        },
        cashAccount (keys) {
            if (keys.some(item => item == 'a')) {
                this.a = false;
                this.b = true;
                this.c = true;
                this.e = true;

                this.d = false;

            } else if (keys.some(item => item == 'b')) {
                this.a = true;
                this.b = false;

                this.c = true;

                this.d = false;
                this.e = false;
            } else if (keys.some(item => item == 'c')) {
                this.a = true;
                this.b = true;
                this.c = false;
                this.d = true;
                this.e = true;
            } else {
                this.a = false;
                this.b = false;
                this.c = false;
                this.d = true;
                this.e = true;
            }
            if (!keys.some(item => item == 'a') && !keys.some(item => item == 'b')) {
                this.cashAccountCheck = this.cashAccountCheck.filter(item => item !== 'd' && item !== 'e');
            }
        },
        auxiliaryCheck () {
            // <a-checkbox v-model="editItem.isAuxaccDepartment" :disabled="auxiliaryA" @change="auxiliaryCheck('a')">部门核算</a-checkbox>
            // <a-checkbox v-model="editItem.isAuxaccPerson" :disabled="auxiliaryB" @change="auxiliaryCheck('b')">个人往来</a-checkbox>
            // <a-checkbox v-model="editItem.isAuxaccCustomer" :disabled="auxiliaryC" @change="auxiliaryCheck('c')">客户往来</a-checkbox>
            // <a-checkbox v-model="editItem.isAuxaccSupplier" :disabled="auxiliaryD" @change="auxiliaryCheck('d')">供应商往来</a-checkbox>
            if (this.editItem.isAuxaccPerson) {
                //个人往来
                this.editItem.isAuxaccDepartment = false;
                this.editItem.isAuxaccCustomer = false;
                this.editItem.isAuxaccSupplier = false;
                this.auxiliaryA = true;
                this.auxiliaryC = true;
                this.auxiliaryD = true;
            } else {
                this.auxiliaryA = false;
                this.auxiliaryC = false;
                this.auxiliaryD = false;

                if (this.editItem.isAuxaccSupplier) {
                    //供应商往来
                    this.auxiliaryB = true;
                    this.auxiliaryC = true;
                    this.editItem.isAuxaccPerson = false;
                    this.editItem.isAuxaccCustomer = false;
                } else {
                    this.auxiliaryB = false;
                    this.auxiliaryC = false;

                    if (this.editItem.isAuxaccCustomer) {
                        //客户往来
                        this.auxiliaryB = true;
                        this.auxiliaryD = true;
                        this.editItem.isAuxaccPerson = false;
                        this.editItem.isAuxaccSupplier = false;
                    } else {
                        this.auxiliaryB = false;
                        this.auxiliaryD = false;
                    }
                }
                if (this.editItem.isAuxaccDepartment) {
                    //部门核算
                    this.auxiliaryB = true;
                    this.editItem.isAuxaccPerson = false;
                }
            }

            if (this.editItem.isAuxaccPerson && this.editItem.isAuxaccProject) {
                //个人往来，项目核算
                this.auxiliaryC = true;
                this.auxiliaryD = true;

                this.editItem.isAuxaccCustomer = false;
                this.editItem.isAuxaccSupplier = false;
            }
        },
        //table check选择
        selection (keys, row) {
            this.selectedKeys = [];
            this.selectedKeys = keys;
            this.selectedData = row[0];
            const ids = new Array();
            row.forEach(function (val) {
                ids.push(val.id);
            });
            this.selectedIds = ids;
            console.log("rowKeys", this.selectedData, keys);
        },
        //上级table选择
        parentSelection (keys, row) {
            console.log(row)
            this.parentSelectedKeys = [];
            this.parentSelectedKeys = keys;
            this.parentSelectedData = row[0];
        },
        selectParentData () {
            if (this.parentSelectedKeys.length != 1) {
                this.$message.error("只能选择一条数据");
                return;
            }
            this.editItem.parentId = this.parentSelectedData.id;
            this.editItem.parentName = this.parentSelectedData.name;


            // this.form.setFieldsValue({ code: this.parentSelectedData.code });
            this.editItem.depth = this.parentSelectedData.depth + 1;

            // this.editItem.accountingTypeId = this.parentSelectedData.accountingTypeId;
            console.log(this.parentSelectedData);
            // return;
            this.accountingTypeName = this.parentSelectedData.accountingTypeName;

            this.form = this.$form.createForm(this, {
                mapPropsToFields: () => {
                    return {
                        accountingTypeId: this.$form.createFormField({
                            value: this.parentSelectedData.accountingTypeId
                        }),
                        code: this.$form.createFormField({
                            value: this.parentSelectedData.code
                        }),
                    };
                }
            });


            this.parentModal = false;
        },
        //获取编码规则
        getCodeDetail () {
            getDetail({ code: 'code' }).then(res => {
                if (res.code == 200) {
                    let code = res.data.codingRule;
                    console.log("code", code);
                    // for (let i = 0; i < code.length; i++) {
                    //     this.ruleCode += code.substring(i, i + 1);
                    //     if (i + 1 < code.length) {
                    //         this.ruleCode += "-";
                    //     }
                    // }
                    this.ruleCode = this.codeStyle(code);
                }
            });
        },
        //处理编码规则样式
        codeStyle (code) {
            let codeText = "";
            for (let i = 0; i < code.length; i++) {
                let codeNum = code.substring(i, i + 1);
                for (let index = 0; index < codeNum; index++) {
                    codeText += "*";
                }
                codeText += " ";
            }
            return codeText;
        },
        //改变分页大小
        changePageSize (e) {
            const value = e.target.value;
            let size = Number.parseInt(value);
            if (!size) {
                size = 20;
            }
            if (size > 500) {
                size = 500;
            }
            Object.assign(this.pagination, {
                current: 1,
                pageSize: size,
            });

            this.PageList();
        },
        handleSearch (key, confirm, col) {
            //table搜索
            confirm();
            console.log(key, confirm, col)
            if (col == 'name') {
                this.searchName = key[0];
                this.searchCode = null;
            } else {
                this.searchCode = key[0];
                this.searchName = null;
            }
            this.PageList()
        },
        handleReset (clearFilters) {
            clearFilters();
            this.searchCode = '';
            this.searchName = '';
            this.PageList()
        },
    }
};
</script>
<style  scoped>
.form-inline label {
    justify-content: left !important;
}

.form-inline>.col {
    margin-bottom: 1.5rem !important;
}

.icons-list>>>.anticon {
    margin-right: 8px;
    font-size: 16px;
}

.ant-advanced-search-form {
    padding: 24px;
    background: #fbfbfb;
}

.ant-advanced-search-form .ant-form-item {
    display: flex;
    margin-bottom: 0px !important;
}

.ant-advanced-search-form .ant-form-item-control-wrapper {
    flex: 1;
}

#components-form-demo-advanced-search .ant-form {
    max-width: none;
}

#components-form-demo-advanced-search .search-result-list {
    margin-top: 16px;
    background-color: #fafafa;
    min-height: 200px;
    text-align: center;
    padding-top: 80px;
}

.ant-checkbox-wrapper+.ant-checkbox-wrapper {
    margin-left: 0px;
}

.ant-checkbox-wrapper {
    margin-top: 10px;
}

.ant-radio-wrapper {
    margin-top: 10px;
}
</style>
<style lang="less">
@import '~@/style/setting/business.less';

.course-table-container {
    // display: flex;
    width: 1200px;
    height: calc(100% - 45px);
    border-bottom: none;
    margin: 0px 10px 0 10px;

    .course-tree-box {
        flex: 0 0 250px;
        height: 100%;
        overflow-y: auto;
        background: #fff;
    }

    .course-table-box {
        width: calc(100% - 250px);
        flex: 1;
        font-size: 12px;
        padding: 10px;
        border-left: 1px solid #ccc;
        overflow-y: auto;

        .ant-pagination {
            padding-left: 20px;
        }
    }
}

.course-ant-modal-body {
    .ant-modal-body {
        padding: 0px;
    }
}

.tabs-course-box {
    position: relative;
}
</style>
 